package com.xn.app.muying.activity;

import android.app.AlertDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.xn.app.muying.R;
import com.xn.app.muying.util.APIMethod;
import com.xn.app.muying.util.BaseActivity;
import com.xn.app.muying.util.MyAlertDialog;
import com.xn.app.muying.util.NetworkUtil;
import com.xn.app.muying.util.ToastUtil;

/**
 * Created by Administrator on 2017/4/18.
 */
public class HealthRecordsDetailsActivity extends BaseActivity {

    //详情ID
    private String detailsID = "";
    //显示数据的webview
    private WebView webview = null;
    //标题文字
    private TextView title_name = null;
    //返回键
    private ImageView img_back = null;
    //加载框
    AlertDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_healthrecords_details);
        //初始化组件
        initialization();
        //实现
        realize();
    }

    /**
     * 初始化组件，给相应组件添加点击事件
     */
    private void initialization() {
        webview = (WebView) findViewById(R.id.webview);
        title_name = (TextView) findViewById(R.id.title_name);
        title_name.setText("报告详情");
        img_back = (ImageView) findViewById(R.id.img_back);
        //返回键单击事件
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                webview.onResume();
                finish();
            }
        });
        //获取上个页面传来的数据
        detailsID = getIntent().getStringExtra("pk");
        Log.i("dasdas",detailsID);
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub

                view.loadUrl(url);
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                return true;
            }

            //页面开始加载
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                dialog = new MyAlertDialog().ShowDialog(HealthRecordsDetailsActivity.this, R.layout.dialog_loading);
                dialog.setCancelable(true);
            }

            //加载结束 （其实页面404等等错误的情况也算加载完成）
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (dialog != null) {
                    dialog.dismiss();
                }
                webview.setVisibility(WebView.VISIBLE);
            }
        });
    }

    /**
     * 功能实现
     */
    private void realize() {

        //获取数据
        if (NetworkUtil.isNetworkAvailable(this)) {
            webview.getSettings().setJavaScriptEnabled(true);
            webview.loadUrl(APIMethod.HealthRecordsDetail(detailsID));
            WebSettings mWebSettings = webview.getSettings();
            mWebSettings.setSupportZoom(true);
            mWebSettings.setLoadWithOverviewMode(true);
            mWebSettings.setUseWideViewPort(true);
            mWebSettings.setDefaultTextEncodingName("GBK");
            mWebSettings.setLoadsImagesAutomatically(true);
        } else {
            ToastUtil.showToast(this, R.string.no_network_hint);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        return super.onKeyDown(keyCode, event);
    }
}
